from redis import asyncio as aioredis
from fastapi import FastAPI


def register_redis(app: FastAPI, config: dict):
    async def redis_pool():
        redis = await aioredis.from_url(
            f"redis://{config.get('username')}:{config.get('password')}@{config.get('host')}:{config.get('port')}/{config.get('db')}",
            decode_responses=True)
        return redis

    @app.on_event("startup")
    async def start_event():
        app.state.redis = await redis_pool()
